package com.ruoyi.client.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.UserLoginForm;
import com.ruoyi.client.pojo.form.UserRegisterForm;
import com.ruoyi.client.pojo.form.UserUpdateForm;
import com.ruoyi.client.pojo.form.UserUpdatePwdForm;
import com.ruoyi.client.pojo.vo.UserLoginVo;
import com.ruoyi.client.pojo.vo.UserRegisterVo;
import com.ruoyi.client.pojo.vo.UserVo;
import com.ruoyi.client.service.UserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
@Anonymous  //接口允许匿名访问
@CrossOrigin
@Api(value = "v1", tags = "02.用户模块")
@RestController("ClientUserController")
@RequestMapping("/user")
public class UserController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;

    /**
     * 用户登录
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "用户登录", notes = "返回用户id、name、avatar、token")
    @ApiImplicitParam(name = "form", value = "用户登录参数", dataType = "UserLoginForm", required = true)
    @PostMapping("/login")
    public R<UserLoginVo> login(@RequestBody @Valid UserLoginForm form) {
        // 用户登录验证
        UserLoginVo userLoginVo = userService.login(form);
        // 用户登录，使用用户ID作为token标识
        StpUtil.login(userLoginVo.getId());
        // 获取登录用户的Token
        String token = StpUtil.getTokenValue();
        // 封装用户登录后返回数据（如果需要返回token，可以添加token字段到UserLoginVo）
        userLoginVo.setToken(token); // 如果需要返回token，取消注释
        // 接口返回
        return R.ok(userLoginVo);
    }

    /**
     * 用户注册
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "用户注册")
    @ApiImplicitParam(name = "form", value = "用户注册参数", dataType = "UserRegisterForm", required = true)
    @PostMapping("/register")
    public R<UserRegisterVo> register(@RequestBody @Valid UserRegisterForm form){
        // 调用服务层
        UserRegisterVo userRegisterVo = userService.register(form);
        // 返回
        return R.ok(userRegisterVo);
    }


    /**
     * 查询我的详情
     */
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "查询我的详情", notes = "查询当前登录用户的详情信息")
    @GetMapping("/detail")
    public R<UserVo> detail() {
        // 调用服务层
        UserVo userVo = userService.getMyDetail();
        // 返回
        return R.ok(userVo);
    }

    /**
     * 用户登出
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "用户登出", notes = "清除token")
    @PostMapping("/logout")
    public R<String> logout() {
        StpUtil.logout();
        return R.ok(); //登出成功
    }

    /**
     * 更改用户信息
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "更改用户信息", notes = "用户id不能为空")
    @ApiImplicitParam(name = "form", value = "用户更改参数", dataType = "UserUpdateForm", required = true)
    @PostMapping("/update")
    public R<String> update(@RequestBody @Valid UserUpdateForm form) {
        return userService.update(form) ? R.ok() : R.fail();
    }

    /**
     * 更改用户密码
     */
    @ApiOperationSupport(order = 32)
    @ApiOperation(value = "更改用户密码", notes = "用户id，新旧密码不能为空")
    @ApiImplicitParam(name = "form", value = "用户更改密码参数", dataType = "UserUpdatePwdForm", required = true)
    @SaCheckLogin
    @PostMapping("/updatePwd")
    public R<String> updatePwd(@RequestBody @Valid UserUpdatePwdForm form) {
        return userService.updatePwd(form) ? R.ok() : R.fail();
    }

}
